var champPREV_ID = "txtPREV_ID";

function VerifyPrevID() {
	var prev_id = document.getElementById(champPREV_ID).value;
	var re = /\d{7}\D{1}/;
	if (prev_id.match(re) && prev_id.length==8) {
		VerifPrevID_modulo(prev_id.toUpperCase());
	} else {
		FlagChamp(champPREV_ID, "Numero de prevoyance non conforme a la regex d{7}D{1}.");
	}
}

function VerifPrevId_modulo(num_CP) {
	var monNum = num_CP.slice(0,-1);
	var maLettre = num_CP.slice(7,8);
	if (LetterToMod(maLettre) == monNum % 23) {
		ClearChamp(champPREV_ID);
	} else {
		FlagChamp(champPREV_ID, "Numero de prevoyance non conforme au modulo 23 de la base numerique.");
	}
}

function LetterToMod(letter) {
	var i = letter.charCodeAt() - 65
	if (i < 0 || i > 26)
		return 99;
	return i;
}

function ModToLetter(modulo) {
	return String.fromCharCode(modulo + 65);
}

function FlagChamp (nomChamp, toolTip) {
	var o =  document.getElementById(nomChamp);
	o.setAttribute("style","background-color:red;");
	o.setAttribute("title", toolTip)
}

function ClearChamp(nomChamp) {
	var o =  document.getElementById(nomChamp);
	o.setAttribute("style","");
	o.setAttribute("title", "")
}
